<!-- 游戏编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate ? '修改用户签到' : '添加用户签到'"
    :visible="visible"
    width="600px"
    :destroy-on-close="true"
    :lock-scroll="false"
    :before-close="confirmClose"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
      <el-form-item label="指定日期:" prop="time_range">
            <el-date-picker
              style="width: 100%"
              v-model="form.time_range"
              type="daterange"
              value-format="yyyy-MM-dd"
              label-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>

      <el-form-item label="积分奖励:" prop="score">
        <el-input
          v-model="form.score"
          placeholder="请输入积分奖励"
          clearable
        />
      </el-form-item>
      <el-form-item label="上架状态:" prop="status">
        <el-radio-group
          v-model="form.status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="2">否</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="备注:" prop="note">
        <el-input v-model="form.note" placeholder="请输入备注" clearable />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="confirmClose()">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "GameCateEdit",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    editData: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({ status: 1 }, this.editData),
      where: {},
      // 表单验证规则
      rules: {
        time_range: [
          { required: true, message: "请选择添加时间", trigger: "blur" }
        ],
        score: [
          { required: true, message: "请输入积分数", trigger: "blur" }
        ],
        status: [
          {
            required: true,
            message: "请选择状态",
            trigger: "blur"
          }
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    "form.channel_id"(val) {
      if (val === 0) {
        this.$delete(this.form, "pay_notify_url");
      } else {
        this.$set(this.form, "pay_notify_url", "");
      }
    },
    visible(val) {
      if (val) {
        if (this.editData) {
          this.form = Object.assign({},{
            ...this.editData,
            time_range:[this.editData.start_time,this.editData.end_time]
          });
          this.isUpdate = true;
        } else {
          this.form = {};
          this.isUpdate = false;
        }
      }
    }
  },
  methods: {
    /* 保存编辑 */
    // changeAppid(val) {
    //   this.$set(this.form, 'app_id', val)
    //   console.log(this.form)
    // },
    save() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$http
            .post("/boxSetScore/edit", this.form)
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    },

    confirmClose() {
      this.$confirm("确定要返回吗？", "温馨提示")
        .then(() => {
          this.updateVisible(false);
          this.initForm();
        })
        .catch(() => {});
    }
  }
};
</script>

<style scoped></style>
